package com.stefanini.pe.bbva.mbl.in.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.stefanini.pe.bbva.mbl.in.dao.iface.FiltroDAO;

public class FiltroDAOImpl extends SqlMapClientDaoSupport implements FiltroDAO {

    public FiltroDAOImpl() {
        super();
    }
    
    public List ejecutarConsultaFiltro(String sentencia , String tipo){
    	List result = null;
    	List column = null;
    	List detalle = null;    	
    	HashMap map = null;
    	List rows = null;
    	HashMap mapResult = null;
		List subDetalle = null;
		Set setMap = null;
		Iterator ite = null;
		Map.Entry entry = null; 
		
		logger.info("Inicio");
		
		result = new ArrayList();
    	column = new ArrayList();
    	detalle = new ArrayList();    		
    	map = new HashMap();
    	
    	map.put("pi_sentencia", sentencia);
    	map.put("pi_tipo", tipo);
    	
    	rows = getSqlMapClientTemplate().queryForList("FILTRO.ejecutar_consulta_filtro", map);
    	 
    	for(int i = 0; i < rows.size(); i++){
    		mapResult = (HashMap)rows.get(i);
    		subDetalle = new ArrayList();
    		setMap = mapResult.entrySet();
    		ite = setMap.iterator();	    		
    		
    		while(ite.hasNext()){	    			
    			entry = (Map.Entry)ite.next();
    			
    			if(i == 0) {
    				column.add(entry.getKey() + "");
    			}	    	
    			
    			subDetalle.add(entry.getValue() + "");	    			
    		}	
    		
    		detalle.add(subDetalle);
    	}	
    	
    	result.add(column);
    	result.add(detalle);
    	
    	logger.info("Fin");    	
    	
    	return result;    	
    }
}